package com.kaka.bus.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.kaka.bus.R;

/**
 * Created by zhangqi on 2017/12/25.
 * e-mail : ${email}
 * desc :
 */

public class DefaultMessageDialog extends Dialog {

    private Activity mActivity;

    public DefaultMessageDialog(@NonNull Context context) {
        this(context, R.style.capitaldialog);
    }

    public DefaultMessageDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        mActivity = (Activity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_message);
        /**
         * 点击返回或者外部是否消失
         */
        setCanceledOnTouchOutside(false);

        WindowManager windowManager = mActivity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();

        Window window = getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();
        //设置居中
        window.setGravity(Gravity.CENTER);

        lp.y = dp2px(mActivity.getApplicationContext(),0);

        lp.alpha=1.0f;
        /**
         * 宽度为屏幕的0.65
         */
        lp.width = (int) (display.getWidth() * 0.65);
        setCanceledOnTouchOutside(false);
        getWindow().setAttributes(lp);

        initView();
    }

    private void initView() {

    }

    public static int dp2px(Context context, int dip) {
        final float scale =  context.getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

}
